/*=============================================================================

  Application UI MODULE

  FILE: UI_evt.cpp
  
  DESCRIPTION

    Application UI Module

  EXTERNALIZED FUNCTIONS


   Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who       what, where, why
  ---------  -------  -------------------------------------------------------
   03/14/04   nso4th    Initial version

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <Cust_AppDef.h>
#include <mainView.h>
/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/

/*=============================================================================
  STRUCTURE DEFINITION
=============================================================================*/

/*=============================================================================
  LOCALIZED FUNCTION IMPLEMENTATION
=============================================================================*/

/*=============================================================================
  EXTERNALIZED FUNCION DEFINITION
=============================================================================*/
// BUTTON VIEW
void UIbtn_Style(GBtn *bn, dword style, dword color)
{
	if(bn == NULL)
		return;

	if(::IsWindow(bn->m_hWnd) == FALSE)
		return;

	if(bn->IsBlinking())
	{
		bn->StopBlinking();
	}
	
	switch(style)
	{
	case UI_BTN_NORMAL_ST:
	{
		bn->EnableWindow(TRUE);
		bn->SetLedColor(GC_GRAY_DARK);
		bn->SetLedColorPushed(color);
		break;
	}
	case UI_BTN_DISABLE_ST:
	{
		bn->EnableWindow(FALSE);
		break;
	}
	case UI_BTN_SELECT_ST:
	{
		bn->EnableWindow(TRUE);
		bn->SetLedColor(color);
		bn->SetLedColorPushed(color);
		break;
	}
	case UI_BTN_BLINK_ST:
	{
		bn->EnableWindow(TRUE);
		bn->StartBlinking(500);
		bn->SetLedColor(GC_GRAY_DARK);
		bn->SetLedColorPushed(color);
		break;
	}
	}
}

void UIbtn_Text(GBtn *bn, char *str, dword color)
{
	if(bn == NULL)
		return;

	if(::IsWindow(bn->m_hWnd) == FALSE)
		return;

	if(color != UI_BTN_NOCOLOR)
	{
		if(str == NULL)
		{
			bn->SetTextColor(color, TRUE);
		}
		else
		{
			bn->SetTextColor(color, FALSE);
		}
	}
	if(str != NULL)
	{
		bn->SetText(str, TRUE);
	}
}


// LED View
void UILed_Style(GLed *ln/*UILed_Style_e*/, dword style)
{
	if(ln == NULL)
		return;

	if(::IsWindow(ln->m_hWnd) == FALSE)
		return;

	if(style != UI_LED_REDBLINK_ST)
	{
		if(ln->IsBlinking())
		{
			ln->BlinkStop();
		}
	}
		
	switch(style)
	{
	case UI_LED_GRAY_ST:
	{
		if(ln->GetColor() != GLED_GRAY)
		{
			ln->SetColor(GLED_GRAY);
		}
		break;
	}
	case UI_LED_YELLOW_ST:
	{
		if(ln->GetColor() != GLED_YELLOW)
		{
			ln->SetColor(GLED_YELLOW);
		}
		break;
	}
	case UI_LED_GREEN_ST:
	{
		if(ln->GetColor() != GLED_GREEN)
		{
			ln->SetColor(GLED_GREEN);
		}
		break;
	}
	case UI_LED_RED_ST:
	{
		if(ln->GetColor() != GLED_RED)
		{
			ln->SetColor(GLED_RED);
		}
		break;
	}
	case UI_LED_BLUE_ST:
	{
		if(ln->GetColor() != GLED_BLUE)
		{
			ln->SetColor(GLED_BLUE);
		}
		break;
	}
	case UI_LED_REDBLINK_ST:
	{
		if(ln->GetColor() != GLED_RED)
		{
			ln->SetColor(GLED_RED);
		}
		if(ln->IsBlinking() == FALSE)
		{
			ln->SetColor(GLED_RED);
			ln->BlinkStart(500);
		}
		break;
	}
	}
}

void UILtxt_Style(GLText *ln/*UILed_Style_e*/, dword style)
{
	if(ln == NULL)
		return;

	if(::IsWindow(ln->m_hWnd) == FALSE)
		return;

	if((style != UI_LED_REDBLINK_ST) && (style != UI_LED_GREENBLINK_ST))
	{
		if(ln->IsBlinking())
		{
			ln->BlinkStop();
		}
	}
		
	switch(style)
	{
	case UI_LED_GRAY_ST:
	{
		if(ln->GetLedColor() != GLED_GRAY)
		{
			ln->SetLedColor(GLED_GRAY);
		}
		break;
	}
	case UI_LED_YELLOW_ST:
	{
		if(ln->GetLedColor() != GLED_YELLOW)
		{
			ln->SetLedColor(GLED_YELLOW);
		}
		break;
	}
	case UI_LED_GREEN_ST:
	{
		if(ln->GetLedColor() != GLED_GREEN)
		{
			ln->SetLedColor(GLED_GREEN);
		}
		break;
	}
	case UI_LED_RED_ST:
	{
		if(ln->GetLedColor() != GLED_RED)
		{
			ln->SetLedColor(GLED_RED);
		}
		break;
	}
	case UI_LED_BLUE_ST:
	{
		if(ln->GetLedColor() != GLED_BLUE)
		{
			ln->SetLedColor(GLED_BLUE);
		}
		break;
	}
	case UI_LED_REDBLINK_ST:
	{
		if(ln->GetLedColor() != GLED_RED)
		{
			ln->SetLedColor(GLED_RED);
		}
		if(ln->IsBlinking() == FALSE)
		{
			ln->SetLedColor(GLED_RED);
			ln->BlinkStart(500);
		}
		break;
	}
	case UI_LED_YELLOWBLINK_ST:
	{
		if(ln->GetLedColor() != GLED_YELLOW)
		{
			ln->SetLedColor(GLED_YELLOW);
		}
		if(ln->IsBlinking() == FALSE)
		{
			ln->SetLedColor(GLED_YELLOW);
			ln->BlinkStart(500);
		}
		break;
	}
	case UI_LED_GREENBLINK_ST:
	{
		if(ln->GetLedColor() != GLED_GREEN)
		{
			ln->SetLedColor(GLED_GREEN);
		}
		if(ln->IsBlinking() == FALSE)
		{
			ln->SetLedColor(GLED_GREEN);
			ln->BlinkStart(500);
		}
		break;
	}
	}
}

//-------------------------------------
// TEXT
//-------------------------------------
void UItxt_Color(GText *gt, dword txCOL, dword bkCOL)
{
	if(gt == NULL)
		return;

	if(::IsWindow(gt->m_hWnd) == FALSE)
		return;

	gt->SetColor(txCOL, bkCOL);
}

static void ui_btn_change(dword event);
static void sys_btn_change(dword event);
void UIbtn_MainOP(dword type, dword event)
{
	switch(type)
	{
	case UI_DEV:
	{
		ui_btn_change(event);
		break;
	}
	case SYS_MGR:
	{
		sys_btn_change(event);
		break;
	}
	}	
}

/*=============================================================================
  LOCALIZED FUNCTION IMPLEMENTATION
=============================================================================*/
#define BTNCOL_SEL				GC_CYAN
#define BTNCOL_DESEL			GC_GRAY_DARK
#define BTNTXT_DESEL			GC_GRAY_LIGHT

void ui_btn_change(dword event)
{
	CMainView			*mv = (CMainView*)ObjList_Get(VIEW_MAIN);
	dword				cur_vw = UIprm_Get(UI_VWCURRENT_P, 0, 0);
	CFormMainOp			*fm;
	dword				btnSel, j;
	
	fm = &mv->m_frmMOp;
	if(::IsWindow(fm->m_hWnd) == FALSE)
		return;

	if(event != UI_VWCHANGE_E)
		return;
	
	switch(cur_vw)
	{
	case UI_MAIN_VW:
	{
		btnSel = FMO_BTN_MAIN;
		break;
	}
	case UI_MANUAL_VW:
	{
		btnSel = FMO_BTN_MANUAL;
		break;
	}
	case UI_SETUP_VW:
	{
		btnSel = FMO_BTN_SETUP;
		break;
	}
	default:
		return;
	}
	for(j=FMO_BTN_EXIT; j<=FMO_BTN_SETUP; j++)
	{
		if(j == btnSel)
		{
			UIbtn_Text(&fm->m_gBtn[j], NULL, BTNCOL_SEL);
			UIbtn_Style(&fm->m_gBtn[j], UI_BTN_SELECT_ST, BTNCOL_SEL);
		}
		else
		{
			UIbtn_Text(&fm->m_gBtn[j], NULL, BTNTXT_DESEL);
			UIbtn_Style(&fm->m_gBtn[j], UI_BTN_SELECT_ST, BTNCOL_DESEL);
		}
	}
	if(cur_vw == UI_MAIN_VW)
	{
		SYSmgr				*sm = (SYSmgr*)ObjList_Get(SYS_MGR);

		if(sm == NULL)
			return;

		if(sm->State_MajCurrent() == IDL_S)
		{
			UIbtn_Text(&fm->m_gBtn[FMO_BTN_EXIT], "EXIT", GC_WHITE);
			UIbtn_Style(&fm->m_gBtn[FMO_BTN_EXIT], UI_BTN_NORMAL_ST, BTNCOL_SEL);
		}
		else
		{
			UIbtn_Text(&fm->m_gBtn[FMO_BTN_MAIN], "MAIN", BTNCOL_SEL);
			UIbtn_Style(&fm->m_gBtn[FMO_BTN_MAIN], UI_BTN_SELECT_ST, BTNCOL_SEL);
		}
	}
	else
	{
		UIbtn_Text(&fm->m_gBtn[FMO_BTN_MAIN], "MAIN", BTNTXT_DESEL);
	}
}

void sys_btn_change(dword event)
{
	CMainView			*mv = (CMainView*)ObjList_Get(VIEW_MAIN);
	CFormMainOp			*fm;
	SYSmgr				*sm = (SYSmgr*)ObjList_Get(SYS_MGR);
	dword				cur_vw = UIprm_Get(UI_VWCURRENT_P, 0, 0);
	dword				mj_sts, mn_sts;

	if(sm == NULL)
		return;
	
	fm = &mv->m_frmMOp;
	if(::IsWindow(fm->m_hWnd) == FALSE)
		return;
	
	mj_sts = sm->State_MajCurrent();
	mn_sts = sm->State_MinCurrent();

	if((event != CHANGE_MAJSTATE_E) && (event != CHANGE_MINSTATE_E))
	{
		return;
	}

	switch(mj_sts)
	{
	case IDL_S:
	{
		UIbtn_Text(&fm->m_gBtn[FMO_BTN_STOP], NULL, GC_RED);
		UIbtn_Text(&fm->m_gJBtn[FMO_BTN_RESET], NULL, GC_YELLOW);
		switch(mn_sts)
		{
		case IDL_ERR_S:
		{
			UIbtn_Style(&fm->m_gBtn[FMO_BTN_STOP], UI_BTN_BLINK_ST, GC_RED);
			UIbtn_Style(&fm->m_gJBtn[FMO_BTN_RESET], UI_BTN_BLINK_ST, GC_YELLOW);
			break;
		}
		case IDL_IDL_S:
		{
			UIbtn_Style(&fm->m_gBtn[FMO_BTN_STOP], UI_BTN_NORMAL_ST, GC_RED);
			UIbtn_Style(&fm->m_gJBtn[FMO_BTN_RESET], UI_BTN_NORMAL_ST, GC_YELLOW);
			break;
		}
		}
		break;
	}
	case DEV_S:
	{
		UIbtn_Style(&fm->m_gBtn[FMO_BTN_STOP], UI_BTN_NORMAL_ST, GC_RED);
		UIbtn_Style(&fm->m_gJBtn[FMO_BTN_RESET], UI_BTN_NORMAL_ST, GC_YELLOW);
		break;
	}
	case MANUAL_S:
	default:
	{
		UIbtn_Style(&fm->m_gBtn[FMO_BTN_STOP], UI_BTN_SELECT_ST, GC_RED);
		UIbtn_Style(&fm->m_gJBtn[FMO_BTN_RESET], UI_BTN_NORMAL_ST, GC_GRAY);

		UIbtn_Style(&fm->m_gBtn[FMO_BTN_RUN], NULL, GC_RED);
		UIbtn_Style(&fm->m_gJBtn[FMO_BTN_RUN], UI_BTN_BLINK_ST, GC_GREEN);
		break;
	}
	}

	if(cur_vw == UI_MAIN_VW)
	{
		if(sm->State_MajCurrent() == IDL_S)
		{
			UIbtn_Text(&fm->m_gBtn[FMO_BTN_EXIT], "EXIT", GC_WHITE);
			UIbtn_Style(&fm->m_gBtn[FMO_BTN_EXIT], UI_BTN_NORMAL_ST, BTNCOL_SEL);
		}
		else
		{
			UIbtn_Text(&fm->m_gBtn[FMO_BTN_MAIN], "MAIN", BTNCOL_SEL);
			UIbtn_Style(&fm->m_gBtn[FMO_BTN_MAIN], UI_BTN_SELECT_ST, BTNCOL_SEL);
		}
	}
	else
	{
		UIbtn_Text(&fm->m_gBtn[FMO_BTN_MAIN], "MAIN", BTNTXT_DESEL);
	}
}

/*--- END OF UIapp_btn_vw.cpp ---*/

